package cn.wolfcode.Controller;


import cn.hutool.core.util.RandomUtil;
import cn.wolfcode.domain.Exam;
import cn.wolfcode.qo.*;
import cn.wolfcode.service.IExamService;
import cn.wolfcode.service.IQuestionService;
import cn.wolfcode.util.RequirePermission;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.beans.BeanInfo;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Controller
@RequestMapping("/exam")
public class ExamController {

    @Autowired
    private IExamService examService;

    @Autowired
    private IQuestionService questionService;


    @RequirePermission(name = "考试表页面",experssion = "exam:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        Exam exams = examService.selectByExam(14L);
        model.addAttribute("exams",exams);
        return "exam/list";
    }

    @RequestMapping("/delete")
@RequirePermission(name = "考试表页面",experssion = "exam:list")
    @ResponseBody
    public jsonResult delete(Long id){
        if (id != null) {
            examService.delete(id);
        }
        return new jsonResult();
    }


    @RequestMapping("/input")
    @RequirePermission(name = "考试表页面",experssion = "exam:saveOrUpdate")
    public String input(){
        return "exam/input";
    }


    @RequestMapping("/data")
    @RequirePermission(name = "考试表页面",experssion = "exam:saveOrUpdate")
    public String data(Model model){
        Exam exams = examService.selectByExam(12L);
        System.out.println(examService.selectByExam(12L));
        model.addAttribute("exams",exams);
        return "exam/list";
    }


    @RequestMapping("/insert")
@RequirePermission(name = "考试表页面",experssion = "exam:saveOrUpdate")
    public String insert(ExamObject e,Model model){
         //新增试卷表的数据
        Exam exam = new Exam(e.getTitle(),e.getExam_minute(),e.getTotal_score());
        examService.save(exam);
        //根据对应的选择类型随机生成题目
        //建立试卷和表的中间表插入
        questionService.creatQuestion(exam.getId(), e.getSingleChoice(), e.getSingleChoice_score(), e.getMultipleChoice(), e.getMultipleChoice_score(), e.getJudgeChoice(), e.getJudgeChoice_scroe());
        Exam exams = examService.selectByExam(exam.getId());
        model.addAttribute("exams",exams);
        return "exam/list";
    }


}
